package com.wisdomstar.ws.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.wisdomstar.common.exception.base.BaseException;
import com.wisdomstar.ws.domain.Category;
import com.wisdomstar.ws.mapper.CategoryMapper;
import com.wisdomstar.ws.service.CategoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author RinMi
 * @since 2023/6/5
 */

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    @Override
    public int insertCategory(Category category) {
        int count = categoryMapper.checkCategoryName(category.getCategoryId(), category.getCategoryName());
        if (count != 0) {
            throw new BaseException("类别名称已存在");
        }
        return categoryMapper.insertCategory(category);
    }

    @Override
    public int updateCategory(Category category) {
        int count = categoryMapper.checkCategoryName(category.getCategoryId(), category.getCategoryName());
        if (count != 0) {
            throw new BaseException("类别名称已存在");
        }
        return categoryMapper.updateCategory(category);
    }

    @Override
    public int removeCategory(Long categoryId) {
        Category category = new Category();
        category.setCategoryId(categoryId);
        return categoryMapper.removeCategory(category);
    }

    @Override
    public PageInfo<Category> listCategory(Category category, Integer pageNum, Integer pageSize) {
        PageMethod.startPage(pageNum, pageSize);
        List<Category> categoryList = categoryMapper.listCategory(category);
        return new PageInfo<>(categoryList);
    }
}




